﻿#include "pch.h"
#include "CEventReactor.h"
#ifdef _WIN32_
#include <windows.h>
#endif

CEventReactor::CEventReactor() :m_hEvent(nullptr), m_bOpen(false)
{}
CEventReactor::~CEventReactor()
{}
int CEventReactor::Notify()
{
	if (!m_bOpen)return NOTIFY_EVENT_STATUS::FAIL;
	int bNotify = 0;
#ifdef _WIN32_
	bNotify = ::SetEvent(m_hEvent);
#endif
	return bNotify ? NOTIFY_EVENT_STATUS::SUCCESS : NOTIFY_EVENT_STATUS::FAIL;
}
int CEventReactor::Wait(long timeMls)
{
	if (!m_bOpen)return WAIT_EVENT_STATUS::WAIT_FAIL;
	int iWaitStatus = WAIT_EVENT_STATUS::WAIT_FAIL;
#ifdef _WIN32_
	DWORD dReturn = ::WaitForSingleObject(m_hEvent, timeMls); //等待事件通知
	switch (dReturn)
	{
	case WAIT_OBJECT_0:iWaitStatus = WAIT_EVENT_STATUS::ACTIVATE; break;
	case WAIT_TIMEOUT:iWaitStatus = WAIT_EVENT_STATUS::TIME_OUT; break;
		//case WAIT_ABANONED​:iWaitStatus = WAIT_EVENT_STATUS::WAIT_FAIL; break;
	case WAIT_FAILED:iWaitStatus = WAIT_EVENT_STATUS::WAIT_FAIL; break;
	default:iWaitStatus = WAIT_EVENT_STATUS::WAIT_FAIL; break;
	}
#endif
	return iWaitStatus;
}
bool CEventReactor::Open()
{
	if (m_bOpen)return false;
#ifdef _WIN32_
	m_hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
#endif
	return (m_bOpen = true);
}
void CEventReactor::Close()
{
	if (m_bOpen)
	{
#ifdef _WIN32_
		if (m_hEvent)
		{
			::CloseHandle(m_hEvent);
			m_hEvent = nullptr;
		}
#endif
	}
}